package com.cloudcc.mobile.view.activity;

//详情页面下面的排序
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.cloudcc.cloudframe.adapter.CommonAdapter;
import com.cloudcc.cloudframe.adapter.ViewHolder;
import com.cloudcc.cloudframe.util.StringUtils;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.entity.beau.BeauListFilterEntity.SearchSort;
import com.cloudcc.mobile.view.base.BaseActivity;

import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

public class BeauSortActivity extends BaseActivity implements
		OnItemClickListener {

	@Bind(R.id.listview)
	ListView mSortListView;

	@Bind(R.id.header_left)
	ImageView header_left;

	@Bind(R.id.header_right)
	TextView header_right;

	@Bind(R.id.header_title)
	TextView header_title;

	private List<SearchSort> mFilterData;
	private boolean isAsc;

	private SortAdapter sortAdapter;

	private String sortSelectName;
	private String sortName;

	@Override
	public void init(Bundle savedInstanceState) {
		super.init(savedInstanceState);
		mFilterData = (List<SearchSort>) getIntent().getSerializableExtra(
				"sort.filter");
		isAsc = getIntent().getBooleanExtra("sort.asc", true);
		sortSelectName = getIntent().getStringExtra("sort.name");
		String title = getIntent().getStringExtra("sort.title");
		if (!TextUtils.isEmpty(title)) {
			TextView tvTitle = (TextView) findViewById(R.id.header_title);
			tvTitle.setText(title);
		}
		sortAdapter = new SortAdapter(mContext);
		sortAdapter.changeData(mFilterData);
		mSortListView.setAdapter(sortAdapter);
		mSortListView.setOnItemClickListener(this);
		addlistener();
	}
	public void addlistener(){
		header_right.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.putExtra("sort.name", sortSelectName);
				intent.putExtra("sort.asc", isAsc);
				intent.putExtra("sort.namezh", sortName);
				setResult(Activity.RESULT_OK, intent);
				finish();
			}
		});
		header_left.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				setResult(Activity.RESULT_CANCELED);
				finish();
			}
		});
	}
	@OnClick(R.id.container1)
	public void clickFrame() {
		this.finish();
	}

	@Override
	public int getLayoutId() {
		return R.layout.beau_search_sort;
	}

	private class SortAdapter extends CommonAdapter<SearchSort> {

		public SortAdapter(Context mContext) {
			super(mContext);
		}

		@Override
		public void convert(ViewHolder holder, SearchSort t) {
			TextView title = holder.getView(R.id.tv1);
			title.setTextColor(Color.BLACK);
			title.setText(t.label);
			ImageView iamge = holder.getView(R.id.iv1);
			if (StringUtils.equals(sortSelectName, t.name)) {
				if (isAsc) {
					iamge.setImageResource(R.drawable.arrow_up);
				} else {
					iamge.setImageResource(R.drawable.arrow_down);
				}
			} else {
				iamge.setImageDrawable(new ColorDrawable(Color.TRANSPARENT));
			}
		}

		@Override
		public int getLayoutId() {
			return R.layout.beau_list_filter_childitem;
		}

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		SearchSort searchSort = sortAdapter.getItem(position);
		if (StringUtils.equals(searchSort.name, sortSelectName)) {
			isAsc = !isAsc;
		}
		sortSelectName = searchSort.name;
		sortAdapter.notifyDataSetChanged();
		sortName = searchSort.label;
	}

	public void clickCancel(View v) {
		setResult(Activity.RESULT_CANCELED);
		this.finish();
	}

	public void clickFn(View v) {
		Intent intent = new Intent();
		intent.putExtra("sort.name", sortSelectName);
		intent.putExtra("sort.asc", isAsc);
		intent.putExtra("sort.namezh", sortName);
		setResult(Activity.RESULT_OK, intent);
		this.finish();
	}
}
